<?php
namespace app\appclass\model;
use app\BaseModel;
use app\card\model\Company;
use app\shop\model\AdminCompany;
use app\shop\model\IndexUser;
use app\shop\model\IndexUserInfo;
use think\facade\Db;



class ClassList extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_appclass_class_list';


    protected $append = [


        'lable',

        'coach',

        'class_card'


    ];


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 18:09
     * @功能说明:支持的会员卡
     */
    public function getClassCardAttr($value,$data){

        if(!empty($data['id'])&&!empty($data['store_id'])){

            $class_card_model = new ClassCard();

//            $dis = [
//
//                'class_id' => $data['id']
//            ];
//
//            $list = $class_card_model->where($dis)->select()->toArray();

            $dis = [

                'a.class_id' => $data['id'],

                'b.store_id' => $data['store_id'],

                'b.status'   => 1
            ];

            $list = $class_card_model->alias('a')
                ->join('longbing_card_appclass_card_list b','a.card_id = b.id')
                ->where($dis)
                ->field('a.*')
                ->select()
                ->toArray();

            return $list;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 10:55
     * @功能说明:绑定标签
     */
    public function getLableAttr($value,$data){

        if(!empty($data['id'])){

            $lable_model  = new ClassLable();

            $list = $lable_model->where(['class_id'=>$data['id']])->select()->toArray();

            return $list;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-12 10:55
     * @功能说明:绑定教练
     */
    public function getCoachAttr($value,$data){

        if(!empty($data['id'])&&!empty($data['store_id'])){

            $coach_model   = new ClassCoach();

            $company_model = new AdminCompany();

            $company = $company_model->companyLow($data['store_id']);

            $company[] = $data['store_id'];

            $dis = [

               // 'b.company_id' => $data['store_id'],

                'a.class_id'   => $data['id'],

                'b.class_status'=> 1,
            ];

            $list = $coach_model->alias('a')
                    ->join('longbing_card_user_info b','a.coach_id = b.fans_id')
                    ->where($dis)
                    ->where('b.company_id','in',$company)
                    ->field('a.*')
                    ->group('b.fans_id')
                    ->select()
                    ->toArray();

            return $list;

        }

    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 09:47
     * @功能说明:后台添加
     */
    public function adminDataAdd($data){

        $update = $data;

        if(isset($data['coach'])){

            unset($data['coach']);
        }

        if(isset($data['lable'])){

            unset($data['lable']);
        }

        if(isset($data['class_card'])){

            unset($data['class_card']);
        }


        $res = $this->dataAdd($data);

        $id  = $this->getLastInsID();

        $res = $this->updateSome($id,$update);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 09:54
     * @功能说明:
     */
    public function updateSome($id,$data){

        $coach_model = new ClassCoach();

        $lable_model = new ClassLable();

        $card_model  = new ClassCard();

        $event_model = new \app\appclass\server\CardList();
        //绑定名片
        $event_model->addObserver($card_model);
        //绑定标签
        $event_model->addObserver($lable_model);
        //绑定教练
        $event_model->addObserver($coach_model);

        $event_model->notify($data,$id);

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 10:50
     * @功能说明:后台编辑
     */
    public function adminDataUpdate($dis,$data){

        $update = $data;

        if(isset($data['coach'])){

            unset($data['coach']);
        }

        if(isset($data['lable'])){

            unset($data['lable']);
        }

        if(isset($data['class_card'])){

            unset($data['class_card']);
        }

        $res = $this->dataUpdate($dis,$data);

        $res = $this->updateSome($dis['id'],$update);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->where('status','>',-1)->order('top desc,id desc')->paginate($page)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['coach_name'] = !empty($v['coach'])?implode(',',array_column($v['coach'],'coach_name')):'';

            }

        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-14 11:10
     * @功能说明:获取门店id
     */
    public function getStoreId($staff_id){

        $user_info = new IndexUserInfo();

        $company_model = new Company();

        $company_id = $user_info->where(['fans_id'=>$staff_id])->value('company_id');

        $top_id = $company_model->where(['id'=>$company_id])->value('top_id');

        $data = !empty($top_id)?$top_id:$company_id;

        return $data;
    }





    /**
     * @author chenniang
     * @DataTime: 2021-06-04 14:30
     * @功能说明:月排课分析
     */
    public function monthClassDate($dis,$page = 10){

        $data = $this->alias('a')
            ->join('longbing_card_appclass_class_date b','a.id = b.class_id')
            ->where($dis)
            ->where('b.status','>',-1)
            ->whereMonth('b.start_time')
            ->field('a.*,count(b.id) as date_count,SUM(b.num) as total_num,SUM(b.have_num) as have_num')
            ->group('a.id')
            ->order('date_count desc,a.id desc')
            ->paginate($page)
            ->toArray();

        return $data;

    }
    /**
     * @author chenniang
     * @DataTime: 2021-06-04 14:30
     * @功能说明:教练月排课分析
     */
    public function coachMonthClassDate($dis,$page = 10){

        $user_model = new IndexUser();

        $data = $user_model->alias('a')
                ->join('longbing_card_appclass_class_date b','a.id = b.coach_id')
                ->where($dis)
                ->where('b.status','>',-1)
                ->whereMonth('b.start_time')
                ->field('a.id,count(b.id) as date_count,SUM(b.num) as total_num,SUM(b.have_num) as have_num,b.cate_id,b.coach_id')
                ->group('a.id')
                ->order('date_count desc,a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;


    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-29 10:37
     * @功能说明:教练/课程月上座率
     */
    public function monthSuccessSite($obj_id,$type=1){

        $date_model = new ClassDate();

        if($type==1){

            $dis['coach_id'] = $obj_id;

        }elseif($type==2){

            $dis['class_id'] = $obj_id;

        }else{

            $dis['store_id'] = $obj_id;
        }

        $dis['status'] = 2;
        //总的容纳人数
        $total_num = $date_model->where($dis)->whereMonth('start_time')->sum('num');
        //上过那些课
        $sign_num  = $date_model->where($dis)->whereMonth('start_time')->sum('have_num');

        return $total_num!=0?round($sign_num/$total_num*100,2):0;


    }





}